package cn.ncist.health_service_provider.service.impl;

import cn.ncist.entity.QueryPageBean;
import cn.ncist.health_interface.service.CheckItemService;
import cn.ncist.health_service_provider.dao.CheckItemMapper;
import cn.ncist.pojo.CheckItem;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@DubboService(interfaceClass = CheckItemService.class,timeout = 50000)
@Transactional
public class CheckItemServiceImpl extends ServiceImpl<CheckItemMapper, CheckItem> implements CheckItemService {

    @Autowired
    private CheckItemMapper checkItemMapper;

    @Override
    public void add(CheckItem checkItem) {
        this.save(checkItem);
    }

    @Override
    public Page<CheckItem> findPage(QueryPageBean queryPageBean) {
        if (queryPageBean.getCurrentPage() == null && queryPageBean.getPageSize() == null) {
            throw new RuntimeException("传参错误");
        }
        Page<CheckItem> pageInfo = new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        LambdaQueryWrapper<CheckItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(queryPageBean.getQueryString() != null && queryPageBean.getQueryString().length() > 0
                    ,CheckItem::getName, queryPageBean.getQueryString())
                .or()
                .eq(queryPageBean.getQueryString() != null && queryPageBean.getQueryString().length() > 0
                    ,CheckItem::getCode, queryPageBean.getQueryString());
        this.page(pageInfo, queryWrapper);
        return pageInfo;
    }

    @Override
    public void delete(Integer id) {
        //检查要删除的检查项是否关联了其他表
        int i = checkItemMapper.findCheckItemInCheckGroupItem(id);
        if (i > 0){
            //检查项关联了其他表
            //先删除关联表中的数据
            checkItemMapper.deleteConnectionTable(id);
        }
        //删除检查项表内的数据
        removeById(id);
    }

    @Override
    public void updateItem(CheckItem checkItem) {
        this.updateById(checkItem);
    }

    @Override
    public List<CheckItem> findAll() {
        List<CheckItem> checkItemList = this.list();
        return checkItemList;
    }
}
